package com.yunsc.admin.controller.product;


import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.product.dto.GoodsServiceDTO;
import com.yunsc.product.entity.YunscGoodsServiceEntity;
import com.yunsc.product.service.YunscGoodsServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 商品服务与承诺记录表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 02:17:43
 */
@Api(tags = "商品信息管理", value = "商品服务与承诺记录接口")
@RestController
@RequestMapping("service")
public class GoodsServiceController {
    @Resource
    private YunscGoodsServiceService goodsServiceService;

    /**
     * 商品服务与承诺列表
     */
    @ApiOperation("查询服务与承诺列表")
    @GetMapping("/list")
    public Result<?> goodsServiceList(@RequestParam(required = false) Map<String, Object> params) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscGoodsServiceEntity> pageUtils = goodsServiceService.queryGoodsServiceList(params, currentStoreId);
        return Result.resultPageList(pageUtils);
    }

    @ApiOperation("查询服务与承诺列表")
    @GetMapping("/all")
    public Result<?> queryGoodsServiceAll() {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        List<YunscGoodsServiceEntity> list = goodsServiceService.queryGoodsServiceAll(currentStoreId);
        return Result.resultList(list);
    }

    /**
     * 保存
     */
    @ApiOperation("添加服务与承诺")
    @RequestMapping("/add")
    public Result<?> addGoodsService(@RequestBody GoodsServiceDTO goodsService) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsServiceService.addGoodsService(goodsService, currentStoreId);
        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改服务与承诺")
    @RequestMapping("/edit")
    public Result<?> editGoodsService(@RequestBody @Validated(ValidationGroups.Update.class) GoodsServiceDTO goodsService) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsServiceService.editGoodsServiceById(goodsService, currentStoreId);
        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除服务与承诺")
    @PostMapping("/delete")
    public Result<?> removeGoodsService(@RequestBody @Validated(ValidationGroups.Delete.class) GoodsServiceDTO goodsService) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsServiceService.removeGoodsServiceById(goodsService.getServiceId(), currentStoreId);
        return Result.ok();
    }

    @ApiOperation("服务上架/下架")
    @PostMapping("/state")
    public Result<?> updateGoodsServiceState(@RequestBody @Validated({ValidationGroups.Update.class}) GoodsServiceDTO goodsService) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsServiceService.updateGoodsServiceState(goodsService, currentStoreId);
        return Result.ok();
    }


    @ApiOperation("根据商品id查询服务与承诺列表")
    @GetMapping("/store/list")
    public Result<?> queryGoodsServiceByGoods(@RequestParam Integer goodsId) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        List<YunscGoodsServiceEntity> serviceList = goodsServiceService.queryGoodsServiceByGoodsId(goodsId, currentStoreId);
        return Result.resultList(serviceList);
    }
}
